﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace John.admin
{
public partial class zsadd : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        //HttpCookie cookie = Request.Cookies["jisuanji110"];
        var session = Session["name"];
        if (session == null)
        {
            Response.Redirect("~/login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            this.DateTimePicker1.Value = DateTime.Now;
            setSelectClass();
            csf();           
        }

    }
    private void setSelectClass()//绑定 类别选框
    {
        DBConn myDB = new DBConn();
        string mySql = "select CID,CName from Category";
        DropDownList3.DataSource = myDB.getDataReader(mySql);
        DropDownList3.DataTextField = "CName";
        DropDownList3.DataValueField = "CID";
        DropDownList3.DataBind();
        myDB.Close();
    }

    private void csf()//绑定 类别选框
    {
        DBConn myDB = new DBConn();
        string mySql = "select ID,CName from csf";
        DropDownList1.DataSource = myDB.getDataReader(mySql);
        DropDownList1.DataTextField = "CName";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
        myDB.Close();
    }   

    protected void Button1_Click(object sender, EventArgs e)
    {      
      
        if (uploadFile.PostedFile.FileName.Trim() != String.Empty && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
        {
            Response.Write("<Script>alert('上传的图片格式必须为.gif或.jpg!!')</Script>");
            return;
        }
        try
        {
            if (uploadFile.PostedFile.FileName.Trim() != String.Empty)
            {
                string nam = uploadFile.PostedFile.FileName;
                //取得文件名(抱括路径)里最后一个"."的索引
                int i = nam.LastIndexOf(".");
                //取得文件扩展名
                string newext = nam.Substring(i);
                //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
                DateTime now = DateTime.Now;
                string newname = now.DayOfYear.ToString() + uploadFile.PostedFile.ContentLength.ToString();
                uploadFile.PostedFile.SaveAs(Server.MapPath(".\\zhaoshang\\" + newname + newext));
                string imgname = newname + newext;             
               
                DBConn mydb = new DBConn();
                string sql =
                    "insert into zhaoshang(zname,zdiqu,zgaikuang,zhezuo,zztze,zscfx,zjjxy,zjzrg,zfbrq,zsftg,zsfsh,zspfl,hyname,zimg) values('" 
                    +TextBox1.Text.Trim()  + "','" 
                    + DropDownList1.SelectedValue  + "','" 
                    + xiangmujianjie.Text.Trim()  + "','" 
                    + DropDownList2.SelectedItem  + "','" 
                    + TextBox2.Text.Trim()  + "','" 
                    + shichang.Text.Trim() + "','" 
                    + jingji.Text.Trim() + "','" 
                    + DateTimePicker1.Value  + "','" 
                    + DateTime.Now + "',0,0,'" 
                    + DropDownList3.SelectedValue   + "','" 
                    + Request.Cookies["jisuanji110"].Value  + "','" 
                    + imgname + "');";
                
                mydb.Insert(sql);
                mydb.Close();
                Response.Redirect("zs_Manage.aspx");
            }
            else
            {
                string sql;
                DBConn mydb = new DBConn();
                sql = "insert into zhaoshang(zname,zdiqu,zgaikuang,zhezuo,zztze,zscfx,zjjxy,zjzrg,zfbrq,zsftg,zsfsh,zspfl,hyname,zimg) values('" 
                +TextBox1.Text.Trim() + "','" 
                + DropDownList1.SelectedValue + "','" 
                + xiangmujianjie.Text.Trim() + "','"
                + DropDownList2.SelectedItem + "','" 
                +TextBox2.Text.Trim() + "','" 
                + shichang.Text.Trim() + "','" 
                + jingji.Text.Trim() + "','" 
                + DateTimePicker1.Value + "','" 
                + DateTime.Now + "',0,0,'" 
                + DropDownList3.SelectedValue + "','" 
                + Request.Cookies["jisuanji110"].Value  + "','showimg.gif');";

                mydb.Insert(sql);
                mydb.Close();
                Response.Redirect("zs_Manage.aspx");
            }
        }
        catch(Exception ex)
        {
            //Response.Redirect("Error.aspx");
            string test = ex.ToString();
        }

    }
  }
}
